﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Doctor
{
    public class DoctorRevivews 
    {
       
        public void ReviewDoctor(int doctorId, int userId, int raitng, string description, ref Model.NutriLeafDBEntities context)
        {
            Model.DoctorReview revivew = new Model.DoctorReview();
            revivew.DoctorProfileID = doctorId;
            revivew.UserID = userId;
            revivew.Rating = raitng;
            revivew.Description = description;
            context.DoctorReviews.AddObject(revivew);
            context.SaveChanges();
        }

        public void RemoveRecentReview(int doctorProfileId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.DoctorReviews.Where(p => p.DoctorProfileID==doctorProfileId).First());
            context.SaveChanges();
        }

        public Model.DoctorReview GetDoctorReviewById(int reviewId, ref Model.NutriLeafDBEntities context)
        {
            return context.DoctorReviews.Where(p => p.DoctorReviewID == reviewId).First();
        }

        public List<Model.DoctorReview> GetDoctorReview(int doctorProfileId, int top, ref Model.NutriLeafDBEntities context)
        {
            return context.DoctorReviews.Where(p => p.DoctorProfileID == doctorProfileId).Take(top).ToList();
        }

        public List<Model.DoctorReview> GetDoctorReviewByDoctor(int doctorProfileId, ref Model.NutriLeafDBEntities context)
        {
            return context.DoctorReviews.Where(p => p.DoctorProfileID == doctorProfileId).ToList();
        }

        public float GetAverageRating(int doctorId, ref Model.NutriLeafDBEntities context)
        {
            float avg=0;
            List<Model.DoctorReview> r = context.DoctorReviews.Where(p => p.DoctorProfileID == doctorId).ToList();
            foreach (Model.DoctorReview rs in r) {
                avg = avg + rs.Rating;

            }
            return (avg / r.Count);
        }

        public int GetReviewCount(int doctorId, ref Model.NutriLeafDBEntities context)
        {
            return context.DoctorReviews.Where(p => p.DoctorProfileID == doctorId).ToList().Count;
        }

        public void RemoveReview(int reviewId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.DoctorReviews.Where(p => p.DoctorReviewID == reviewId).First());
            context.SaveChanges();
        }

        public List<Model.DoctorProfile> GetTopRatedDoctors()
        {
            throw new NotImplementedException();
        }

        public List<Model.DoctorProfile> GetTopRatedDoctors(DoctorType type, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }
    }
}

     